import Taro, { Component } from '@tarojs/taro'
import { View, Image, Text, Swiper, SwiperItem, ScrollView } from '@tarojs/components'
import { Header } from '../common/header';
import { GoodsItem } from '../components/goodsItem'
import './goods.scss';
import '../common/header.scss';
import '../../icon.scss';

export class Goods extends Component {

  constructor(props){
    super(props);
    this.state = {
      goods:[],
    }
  }

  componentWillMount () { }

  componentDidMount () { 
    let that = this;
    let user = Taro.getStorageSync('user');
    Taro.request({
      credentials:'include',
      url: 'https://guoxiaojian.guoxiaoge.cn/api/goods/list?token='+user.token,
      success(res){
        if(res.statusCode === 200){
          if(res.data.status === 'SUCCESS'){
            that.setState({
              goods:res.data.data
            });
          }else{
            if(res.data.status === 'FAILED'){
              if(res.data.failedCode === 'NO_LOGIN'){
                Taro.navigateTo({
                  url:'/pages/login/login'
                });
              }
            }
          }
        }
      }
    })
  }

  handleRedirect(pages){
    if(pages){
      Taro.navigateTo({
        url: pages
      });
    }
  }

  onClickLeftIcon(){
    Taro.navigateBack();
  }

  render () {
    return (
        <View style={{paddingBottom:this.props.mb}}>
          <Header 
            leftIconType='chevron-left' 
            onClickLeftIcon={this.onClickLeftIcon} 
            title='更多商品'>
          </Header>
            {/* goods-item */}
            <ScrollView>
              <View className='index-goods-list'>
                {
                  this.state.goods.map(function(item){
                    return <GoodsItem className="goods-item" goods={item}></GoodsItem>
                  })
                }
              </View>
            </ScrollView>
        </View>
    )
  }
}